package daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.naming.spi.DirStateFactory.Result;

import dao.BaseDao;
import dao.CarDao;
import pojo.Car;

public class CarDaoImpl extends BaseDao implements CarDao {

	@Override
	public int addCar(Car car) {
		Object[] arr=  {car.getChezhuID(),car.getJiage(),car.getPinpai(),car.getYanse(),car.getGoucheshijian(),
				car.getZhongliang(),car.getZuoweishu(),car.getChejiahao(),car.getChuchangshijian(),car.getRanliao(),
				car.getFadongjihao(),car.getChejiahao(),car.getShiyongxingzhi(),car.getPaifangdengji(),car.getNianjianshijian(),
				car.getPinpaixinghao(),car.getJiagoutedian()};
		return executeUpdate("insert into car (id,userid,jiage,pinpai,yanse,goucheshijian,zhongliang,zuoweishu,chejiahao,chuchangshijian,ranliao,fadongjihao,chepaihao,shiyongxingzhi,paifangdengji,nianjianshijian,pinpaixinghao,jiegoutedian)"
				+ " values(default,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",arr);
		
	}

	@Override
	public List<Car> selectCar() {
		Connection connection = BaseDao.getConnecton();
		List<Car> list = new ArrayList<Car>();
		String sql = "select user.name,car.* from car inner join user on car.userid = user.id;";
		try {
			PreparedStatement pstmt =executeQuery(sql);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()) {
				Car car = new Car();
				car.setChezhuname(rs.getString(1));
				car.setJiage(rs.getInt(4));
				car.setPinpai(rs.getString(5));
				car.setYanse(rs.getString(6));
				car.setGoucheshijian(rs.getString(7));
				car.setZhongliang(rs.getString(8));
				car.setZuoweishu(rs.getInt(9));
				car.setChejiahao(rs.getString(10));
				car.setChuchangshijian(rs.getString(11));
				car.setRanliao(rs.getString(12));
				car.setFadongjihao(rs.getString(13));
				car.setChepaihao(rs.getString(14));
				car.setShiyongxingzhi(rs.getString(15));
				car.setPaifangdengji(rs.getString(16));
				car.setNianjianshijian(rs.getString(17));
				car.setPinpaixinghao(rs.getString(18));
				car.setJiagoutedian(rs.getString(19));
				list.add(car);
			}
			return list;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}
